<?php

/**
 * Description of runcode
 *
 * @author srkrishnan
 */

/**

 * @property CI_Loader $load

 * @property CI_Form_validation $form_validation

 * @property CI_Input $input

 * @property CI_Email $email

 * @property CI_DB_active_record $db

 * @property CI_DB_forge $dbforge
 * 
 * @property problem $problem 

 */
class Runcode extends CI_Controller {

    private $userid = "anantha89", $password = "myideone";

    function Runcode() {
        parent::__construct();
    }

    function index() {

        $code = '';

        $languageid = '';

        $input = '';

        $run = true;

        $private = true;


        $code = trim($this->input->post('code'));
        $input = trim($this->input->post('input'));
        $languageid = trim($this->input->post('languageid'));

try{
        //create new SoapClient
        $client = new SoapClient("http://ideone.com/api/1/service.wsdl");

        //create new submission
        $result = $client->createSubmission($this->userid, $this->password, $code, $languageid, $input, $run, $private);

        //if submission is OK, get the status
        if ($result['error'] == 'OK') {

            $status = $client->getSubmissionStatus($this->userid, $this->password, $result['link']);

            if ($status['error'] == 'OK') {

                //check if the status is 0, otherwise getSubmissionStatus again
                while ($status['status'] != 0) {
                    sleep(3); //sleep 3 seconds
                    $status = $client->getSubmissionStatus($this->userid, $this->password, $result['link']);
                }

                //finally get the submission results
                $details = $client->getSubmissionDetails($this->userid, $this->password, $result['link'], true, true, true, true, true);
                if ($details['error'] == 'OK') {
                    //var_dump($details);
                } else {
                    //we got some error
                    //var_dump($details);
                }
            } else {
                //we got some error
                //var_dump($status);
            }
        } else {
            //we got some error
            //var_dump($result);
        }


        $jsonresult = array(
            'result' => $result,
            'status'=>$status,
            'details'=>$details
        );
}
catch(Exception $e){
    $jsonresult=array(
      'error' => $e  
    );
}
        $jsonresult = json_encode($jsonresult);
        echo $jsonresult;
    }

}

?>
